// pages/mine/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    listData: [{
        id: 0,
        iconUrl: '../images/message.png',
        title: '报名的组局'
      },
      {
        id: 1,
        iconUrl: '../images/upvote.png',
        title: '推荐给朋友'
      },
      // {
      //   id: 2,
      //   iconUrl: '../images/interaction.png',
      //   title: '设置'
      // }
    ]
  },

  getUserProfile(e) {
    const that = this
    wx.getUserProfile({
      desc: '获取用户信息',
      success: (res) => {
        getApp().globalData.nickName = res?.userInfo?.nickName
        getApp().globalData.avatarUrl = res?.userInfo?.avatarUrl

        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })

        // 更新用户信息
        that.updateUserInfo(res?.userInfo)
      },
      fail: (res) => {
        that.setData({
          userInfo: {},
          hasUserInfo: false
        })
      }
    })
  },

  updateUserInfo: function (value) {
    let token = getApp().globalData.token;
    wx.cloud.callContainer({
      config: {
        env: "prod-9gno6bd407ac56da"
      },
      path: "api/user/update",
      header: {
        "X-WX-SERVICE": "springboot-kd4g",
        "token": token
      },
      method: "POST",
      data: {
        nickName: value.nickName || '',
        avatarUrl: value.avatarUrl || ''
      },
      success(res) {
        if (res?.data?.code == 0) {
          console.log('用户信息更新成功')
        }
      }
    })
  },

  onItemClick: function (e) {
    let id = e.currentTarget.dataset.value

    if (id == 0) {
      wx.navigateTo({
        url: 'takein/index',
      })
    } else if (id == 1) {

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let nickName = getApp().globalData.nickName;
    if (nickName) {
      this.setData({
        userInfo: getApp().globalData,
        hasUserInfo: true
      })
    } else {
      this.setData({
        userInfo: {},
        hasUserInfo: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      return {
        title: '入群找扬尘',
        path: '/pages/home/index',
        imageUrl: '../images/share_image.png'
      }
    }
  }
})